/**
 * 
 */
package chml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;


/**
 * @author chengmenglong
 *
 */
public class DataProcess {

	static ArrayList<String> dataList = new ArrayList<String>();//用于存储文件中的所有字符串序列
	static HashMap<String, Integer> wcMap = new HashMap<String, Integer>();//用于存储所有字符串对应的出现次数
	static ArrayList<WordCount> wcList = new ArrayList<WordCount>();//存储排序的wordCount列表，用于kwise分区
	
	//获取document文件中的每个字符串并存储起来
	public static String[] getDocumentData(String filename){
		
		File dataFile = new File(filename);
	    if(!dataFile.exists())
	    { 
	        System.err.println("Can't Find " + filename);
		}
		
		try 
		{
		    BufferedReader in = new BufferedReader(new FileReader(dataFile));
		    String str;
		    while ((str = in.readLine()) != null) 
		    {
		    	String [] tempStrings = str.split(" ");
		    	for(String tempString: tempStrings){
		    		//存储字符串序列
		    		dataList.add(tempString);
		    	}
		    }
		    in.close();
		} 
		catch (IOException e) 
		{
		    e.getStackTrace();
		}
		
		String[] retStrings = (String[]) dataList.toArray(new String[dataList.size()]);
		
		return retStrings;
	}

	//获取query文件中的每个字符串并存储起来
	public static String[] getQueryData(String filename){
		
		ArrayList<String> queryList = new ArrayList<String>();//用于存储文件中的所有字符串序列
		File queryFile = new File(filename);
	    if(!queryFile.exists())
	    { 
	        System.err.println("Can't Find " + filename);
		}
		
		try 
		{
		    BufferedReader in = new BufferedReader(new FileReader(queryFile));
		    String str;
		    while ((str = in.readLine()) != null) 
		    {
		    	String [] tempStrings = str.split(" ");
		    	for(String tempString: tempStrings){
		    		//存储字符串序列
		    		queryList.add(tempString);
		    	}
		    }
		    in.close();
		} 
		catch (IOException e) 
		{
		    e.getStackTrace();
		}
		
		String[] retStrings = (String[]) queryList.toArray(new String[queryList.size()]);
		
		return retStrings;
	}
	
	//存储每个字符串出现的频率
	public static void getWordCount(){
		
		for(String tempString: dataList){
			//存储字符串的出现次数
			if(wcMap.containsKey(tempString)){
				int count = wcMap.get(tempString);
				wcMap.replace(tempString, count, count+1);
			}else{
				wcMap.put(tempString, 1);
			}
		}
	}
	
	public static void sortWordCount() {
		
		//从wcmap转化为wclist
		Iterator<Entry<String, Integer>> iterator = wcMap.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<String, Integer> entry = (Entry<String, Integer>)iterator.next();
			WordCount wCount = new WordCount(entry.getKey(), entry.getValue());
			wcList.add(wCount);
		}
		
		//按照字符串出现的频率排序
		ComparatorWordCount cpwCount = new ComparatorWordCount();
		Collections.sort(wcList, cpwCount);
		
		/*for(WordCount wc: wcList){
			System.out.println(wc.getWord() + "-->" + wc.getCount());
		}*/
	}
	
	public static int getTokenClass(String str){
		if(!wcMap.containsKey(str)){
			return 0;
		}
		int count = wcMap.get(str);
		int pti = 1;//返回的分区号
		
		pti = (count + 4) / 5;
		
		return Math.min(pti, 20);
	}
}
